/**
 * 
 */
package t4j.concurrency;
//670
class Sleeper extends Thread {
	private int duration;

	public Sleeper(String name, int sleepTime) {
		super(name);
		duration = sleepTime;
		start();
	}

	public void run() {
		try {
			sleep(duration);
		} catch (InterruptedException e) {
			//被中断了就打印，isInterrupted()在catch总是为false,因为异常被捕获的时候将清理这个标志
			System.out.println(getName() + " was interrupted. isInterrupted(): " + isInterrupted());
			return;
		}

		System.out.println(getName() + " has awakened");
	}
}

class Joiner extends Thread {
	private Sleeper sleeper;

	public Joiner(String name, Sleeper sleeper) {
		super(name);
		this.sleeper = sleeper;
		start();
	}

	public void run() {
		try {
			sleeper.join();//加入另外一个Sleeper线程，等待加入的线程结束，才继续执行本线程
		} catch (InterruptedException e) {
			System.out.println("Interrupted");
		}

		System.out.println(getName() + " join completed");
	}
}

public class Joining {
	public static void main(String[] args) {
		Sleeper sleepy = new Sleeper("Sleepy", 1500), 
				grumpy = new Sleeper("Grumpy", 1500);

		Joiner dopey = new Joiner("Dopey", sleepy), 
				 doc = new Joiner("Doc", grumpy);
		
		grumpy.interrupt();//中断
		System.out.println(grumpy.isInterrupted());

	}

}
